//object for loading AJAX url's
var urlAJAX = function (url, container, callback){
	this.url = url;
	this.container = container;
	this.loadCheck = loadCheck;
	if(callback !== "undefined"){
		this.callback = callback;	
	}
}

//AJAX loading method
var loadCheck = function(){
	if (window.XMLHttpRequest){
		// obtain new object
		obj = new XMLHttpRequest();
		obj.container = this.container
		// set the callback function
		if(this.callback){
			obj.callback = this.callback;	
		}
		obj.onreadystatechange = loadAJAX;
		// we will do a GET with the url; "true" for asynch
		obj.open("GET", this.url, true);
		// null for GET with native object
		obj.send(null);
		// IE/Windows ActiveX object
	}
	else if (window.ActiveXObject){
		obj = new ActiveXObject("Microsoft.XMLHTTP");
		if (obj) {
			obj.onreadystatechange = loadAJAX;
			obj.open("GET", this.url, true);
			// don't send null for ActiveX
			obj.send();
		}
	}
	else{
		alert("Your browser does not support AJAX");
	}	
}

var loadAJAX = function() {
    if (obj.readyState == 4) {
        if (obj.status == 200) {
        	document.getElementById(this.container).innerHTML=obj.responseText;
        	if(this.callback){
        		this.callback();	
        	}
        } else {
            alert("There was a problem in the returned data:\n");
        }
    }
}

function IFrame(parentElement){
	// Create the iframe which will be returned
	var iframe = document.createElement("iframe");
	
	// If no parent element is specified then use body as the parent element
	if(parentElement == null)
	   parentElement = document.body;
	
	// This is necessary in order to initialize the document inside the iframe
	parentElement.appendChild(iframe);
	
	// Initiate the iframe's document to null
	iframe.doc = null;
	
	// Depending on browser platform get the iframe's document, this is only
	// available if the iframe has already been appended to an element which
	// has been added to the document
	if(iframe.contentDocument)
	   // Firefox, Opera
	   iframe.doc = iframe.contentDocument;
	else if(iframe.contentWindow)
	   // Internet Explorer
	   iframe.doc = iframe.contentWindow.document;
	else if(iframe.document)
	   // Others?
	   iframe.doc = iframe.document;
	
	// If we did not succeed in finding the document then throw an exception
	if(iframe.doc == null)
	   throw "Document not found, append the parent element to the DOM before creating the IFrame";
	
	// Create the script inside the iframe's document which will call the
	iframe.doc.open();
	iframe.doc.close();
	
	// Return the iframe, now with an extra property iframe.doc containing the
	// iframe's document
	return iframe;
}

function kseRefresh(){
	try{
		alert("Changes made");
	}
	catch(err){
		txt="There was an error on this page.\n\n";
		txt+="Error description: " + err.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
	}
}

function kseDelelement(type,id,returnpath){
	try{
		if(confirm("Delete item?")){
			/*var dynamicUrl = new urlAJAX(this, "content", customInit);
			dynamicUrl.loadCheck();
			*/
			
			var xmlhttp;
			
			if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
				xmlhttp=new XMLHttpRequest();
			}
			else{// code for IE6, IE5
				alert("Old bwoser no support");
			}

			xmlhttp.onreadystatechange=function(){
				if(xmlhttp.readyState==4 && xmlhttp.status==200){
					//kseLoad("http://kse.caspella/index.php?route=kse_admin/list/&type="+ type +"&path=" + returnpath, "content");
					
					document.getElementById("li-"+type+"-"+id).style.display = "none";
				}
			}
			path = "index.php?route=kse_admin/list/remove/&element="+ id +"&type="+ type;
			xmlhttp.open("GET",path,true);
			xmlhttp.send();
			
		}
	}
	catch(err){
		txt="There was an error on this page.\n\n";
		txt+="Error description: " + err.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
	}
}

function kseMakeForm(){
	if(confirm("Are you sure?")){
		var mycontent=document.getElementById("content");
		//creating iframe
		
		var hiddenframe = new IFrame(mycontent);
		hiddenframe.setAttribute("frameborder","0");
		hiddenframe.setAttribute("id","hiddenframe");
		
		var nativeForm = document.getElementById("templateform");
		var cloneForm = nativeForm.cloneNode(true);
		//appending clone form
		hiddenframe.doc.body.appendChild(cloneForm);
		
		//
		for(var i=0; i < nativeForm.length; i++){
			//appending textarea content
			if(nativeForm[i].type == "textarea"){
				var textareaNode = nativeForm[i].id;
				hiddenframe.doc.getElementById(textareaNode).innerHTML = nativeForm[i].value;
			}
			//appending select
			if(nativeForm[i].nodeName == "SELECT"){
				var tmp = nativeForm[i];
				hiddenframe.doc.getElementById(nativeForm[i].id).value = nativeForm[i].value;
			}
		}
		cloneForm.submit();
	}
}



var expandMe = function(){
	var expando = this.parentNode; 
	if(hasClass(expando, "kadmin-expando-on")){
		removeClass(expando, "kadmin-expando-on");
		addClass(expando, "kadmin-expando-off");
	}
	else{
		removeClass(expando, "kadmin-expando-off");
		addClass(expando, "kadmin-expando-on");
	}
}

var makeExpand = function(elem){
	var menumaincont = document.getElementById(elem);
	var menuconts = getElementsByClass("kadmin-expando", menumaincont);
	for(var i = 0; i < menuconts.length; i++){
		var menucont = menuconts[i];
		var menuconth2 = menucont.getElementsByTagName("h2")[0];
		addClass(menucont, "kadmin-expando-on");
		addEvent(menuconth2, 'click', expandMe);
	}
	
}

var doAnchors = function(){
	var allatags = document.getElementsByTagName("a");
	for(var i = 0; i < allatags.length; i++){
		var atag = allatags[i];
		addEvent(atag, 'click', makeAnchors);
	}
}
	
var makeAnchors = function(){
	var dynamicUrl = new urlAJAX(this, "content", customInit);
	dynamicUrl.loadCheck();
	return false;
}

var customInit = function(){
	doAnchors();
	standardistaTableSortingInit();
	imageLoad();
}

var imageLoad = function(){
	var allatags = document.getElementsByTagName("input");
	for(var i = 0; i < allatags.length; i++){
		var atag = allatags[i];
		if(atag.type === "file"){
			//alert("gota");
			addEvent(atag, 'change', getNewImage);
		}
	}
}

var getNewImage = function(){
	var clonedNode = this.cloneNode(false);
	var fromNode = document.getElementById("photos");
	
	//creating divs for each images
	var iDiv = document.createElement("div");
	iDiv.setAttribute("id","idiv" + mymounct);
	fromNode.appendChild(iDiv);
	addClass(iDiv, "imagecont");

	//creating radio button for default button
	var iRadio = document.createElement("input");
	iRadio.setAttribute("type", "radio");
	iRadio.setAttribute("name", "defimage");
	iRadio.setAttribute("value","photo" + mymounct);
	//checked if first foto
	if(mymounct === 1){
		iRadio.setAttribute("checked", "checked");
	}
	iDiv.appendChild(iRadio);
	//creating label input
	var iLabel = document.createElement("input");
	iLabel.setAttribute("type", "text");
	iLabel.setAttribute("id", "photo" + mymounct);
	iLabel.setAttribute("name", "addphoto[photo" + mymounct + "]");
	iDiv.appendChild(iLabel);
	
	//creating remove button
	var iRemove = document.createElement("img");
	iRemove.setAttribute("src", "images/icons/delete.png");
	iDiv.appendChild(iRemove);
	//adding event 
	addEvent(iRemove, "click", rmParent)
	
	
	clonedNode.setAttribute("name","photo" + mymounct);
	
	iDiv.appendChild(clonedNode);
	
	mymounct++;
	this.value = "";
	this.name="addmore"
}

var rmParent = function(){
	this.parentNode.parentNode.removeChild(this.parentNode);
}

//Init script
var mymounct = 1;
makeExpand("left-menu");
doAnchors();


